package com.link.base.base.appinfo.controller;

import com.link.base.base.appinfo.model.AppVersion;
import com.link.base.base.appinfo.service.AppMobileInfoService;
import com.link.base.base.appinfo.service.AppVersionService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.cllog.LCLogger;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 移动版本相关
 *
 * @author ouyangzhenqiang
 * @date 2018/10/16
 */
@Controller
@RequestMapping("/link/appVersion")
public class AppVersionController extends BasicController<AppVersion> {

    @Resource
    private AppVersionService appVersionService;

    @Resource
    private AppMobileInfoService mobileInfoService;

    @Override
    public BasicService<AppVersion> getBasicService() throws Exception {
        return appVersionService;
    }

    /**
     * 版本发布
     *
     * @author ouyangzhenqiang
     * @date 2018/10/16
     */
    @PostMapping(value = "/publicAppVersion", name = "移动应用版本发布")
    @ResponseBody
    public Map<String, Object> publicAppVersion(@JsonParam AppVersion record, HttpSession session,
                                                HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            appVersionService.publishAppVersion(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 移动端版本检测
     *
     * @param request appId 应用Id
     * @author ouyangzhenqiang
     * @date 2018/10/16
     */
    @GetMapping(value = "/query", name = "移动端版本检测")
    @ResponseBody
    public Map<String, Object> collectClient(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        String appId = request.getParameter("appId");
        try {
            if (StringUtils.isNotBlank(appId)) {
                LCLogger.info().withMessageKey("query").withMessage("版本检测 => appId: " + appId).flush();
                AppVersion appVersion = mobileInfoService.getAppVersion(appId);
                result.put("result", appVersion);
            }
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.toString());
        }
        return result;
    }

    /**
     * 获取历史版本
     *
     * @author ouyangzhenqiang
     * @date 2018/11/19
     */
    @PostMapping(value = "/getAppVersionList", name = "移动应用历史版本")
    @ResponseBody
    public Map<String, Object> getAppVersionList(@RequestBody AppVersion record,
                                                 HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            record.setLoginCorpId(0L);
            List<AppVersion> list = appVersionService.queryByExamplePage(record);
            for (AppVersion item : list) {
                item.setAnonymousFlag("Y");
            }
            result.put("success", true);
            result.put("result", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.toString());
        }
        return result;
    }
}
